﻿<cfscript>
/** 
* Date & Time advice
* 
* @hint "Date & Time advice" 
*/ 

component 
	displayname="public.service.util.datetimeAdvice" 
	output=false 
	{
	
	/**
	* @hint "初始化对象"
	*/
	public public.service.util.datetimeAdvice function init() output=false {
		return this;
	}

	/**
	* @hint "格式化日期时间"
	*/
	public string function getDateTime() output=false {
		return dateFormat(now(), "YYYY年MM月DD日") & " " & timeFormat(now(), "HH:MM:SS");
	}

	/**
	* @hint "格式化日期"
	*/
	public string function getDate() output=false {
		return dateFormat(now(), "YYYY年MM月DD日");
	}
	
	/**
	* @hint "返回一个当前日期时间的字符串 YYYYMMDDHHMISS"
	*/
	public string function getCurrentDatetimeString() output=false {
		return REReplace(now(), "[ts[:punct:][:space:]]", "", "ALL");
	}

	/**
	* @hint "返回一个当前日期的字符串 YYYYMMDD"
	* 
	* @mask "日期格式 默认为 YYYYMMDD"
	*/
	public string function getCurrentDateString( string mask="YYYYMMDD" ) output=false {
		return DateFormat(now(), arguments.mask);
	}

	/**
	* @hint "返回一个当前时间的字符串 HHMMSS"
	* 
	* @mask "时间格式 默认为 HHMMSS"
	*/
	public string function getCurrentTimeString( string mask="HHMMSS" ) output=false {
		return TimeFormat(now(), arguments.mask);
	}

	
	/**
	* @hint "将一个日期时间的字符串转换成格林尼治时间"
	* 
	* @datetimeString "日期时间字符串"
	*/
	public string function convertDatetimeStringToGMT( required string datetimeString ) output=false {
		
		var result = now();
		var fString = "";
		
		try {
			if (isNumeric(arguments.datetimeString)) {
				fString = toString(numberFormat(arguments.datetimeString, "______________"));
			}
			result = getHTTPTimeString(
										createDateTime(
														mid(fString,1,4),
														mid(fString,5,2),
														mid(fString,7,2),
														mid(fString,9,2),
														mid(fString,11,2),
														mid(fString,13,2)
														));
		}
		catch(any excpt) {
			result = "";
		}
		
		return result;
	}
	
	/**
	* @hint "将一个日期时间的字符串转换成日期对象"
	* 
	* @datetimeString "日期时间字符串"
	*/
	public date function convertDatetimeStringToDate( required string datetimeString ) output=false {
		
		var result = now();
		var fString = "";
		
		try {
			if (isNumeric(arguments.datetimeString)) {
				fString = toString(numberFormat(arguments.datetimeString, "______________"));
			}
			result = createDateTime(
									mid(fString,1,4),
									mid(fString,5,2),
									mid(fString,7,2),
									mid(fString,9,2),
									mid(fString,11,2),
									mid(fString,13,2)
									);
		}
		catch(any excpt) {
			result = createDateTime(1970,1,1,0,0,0);
		}
		return result;
	}
	
	/**
	* @hint "将一个日期的字符串转换成日期对象"
	* 
	* @dateString "日期字符串"
	*/
	public date function convertDateStringToDate( required string dateString ) output=false {
		
		var result = now();
		var fString = "";
		
		try {
			if (isNumeric(arguments.dateString)) {
				fString = toString(numberFormat(arguments.dateString, "________"));
			}
			result = createDate(
								mid(fString,1,4),
								mid(fString,5,2),
								mid(fString,7,2)
								);
		}
		catch(any excpt) {
			result = createDate(1970,1,1);
		}
		return result;
	}
	
	/**
	* @hint "格式化时间"
	* 
	* @timeString "时间字符串"
	* @mask "时间格式 HH:MI:SS"
	*/
	public string function formatTimeString( required string timeString, string mask="HH:MI:SS" ) output=false {
		
		var result = "";
		var fString = "";
		
		try {
			
			if (isNumeric(arguments.timeString)) {
				fString = toString(left(arguments.timeString & "000000", 6));
			}
			
			result = replace(arguments.mask, "HH", mid(fString,1,2) );
			result = replace(result, "MI", mid(fString,3,2) );
			result = replace(result, "SS", mid(fString,5,2) );
			
		}
		catch(any excpt) {
			result = "";
		}
		return result;
	}
	
	/**
	* @hint "格式化日期"
	* 
	* @dateString "日期字符串"
	* @mask "日期格式 YYYY-MM-DD"
	*/
	public string function formatDateString( required string dateString, string mask="YYYY-MM-DD" ) output=false {
		
		var result = "";
		var fString = "";

		var fullMonth = ["January","February","March","April","May","June","July","August","September","October","November","December"];
		var shortMonth = ["Jan","Feb","March","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"];
		
		try {
			if (isNumeric(arguments.dateString)) {
				fString = toString(numberFormat(arguments.dateString, "________"));
			}
			
			result = replace(arguments.mask, "YYYY", mid(fString,1,4) );
			result = replace(result, "YY", mid(fString,3,2) );
			
			result = replace(result, "MMMM", fullMonth[mid(fString,5,2)]);
			result = replace(result, "MMM", shortMonth[mid(fString,5,2)]);
			result = replace(result, "MM", mid(fString,5,2) );

			result = replace(result, "DD", mid(fString,7,2) );
		}
		catch(any excpt) {
			result = "";
		}
		return result;
	}

	/**
	* @hint "格式化日期时间字符串"
	* 
	* @datetimeString "日期时间字符串"
	* @mask "日期时间格式 YYYY-MM-DD HH:MI:SS"
	*/
	public string function formatDatetimeString( required string datetimeString, string mask="YYYY-MM-DD HH:MI:SS" ) output=false {
		
		var result = "";
		var fString = "";
		
		var fYear = 0;
		var fMonth = 0;
		var fDay = 0;
		var fWeekDay = 0;
		
		var fullMonth = ["January","February","March","April","May","June","July","August","September","October","November","December"];
		var shortMonth = ["Jan","Feb","March","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"];
		
		var fullWeekday = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
		var shortWeekday = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
		
		try {
			if (isNumeric(arguments.datetimeString)) {
				fString = toString(numberFormat(arguments.datetimeString, "______________"));
			}
			
			fYear = mid(fString,1,4);
			fMonth = mid(fString,5,2);
			fDay = mid(fString,7,2);
			
			if (fMonth eq 1 OR fMonth eq 2) {
				fMonth = fMonth + 12;
				fDay = fDay - 1;
			}
			
			fWeekDay = 1 + (fDay + 2*fMonth + 3*(fMonth+1)/5 + fYear + fYear/4 - fYear/100+fYear/400)%7;
			
			fHour = mid(fString,9,2);
			
			result = trim(arguments.mask);
			
			result = replace(result, "FULLTIME", "DDDD, MMMM, DD, YYYY HH:MI:SS TT");
			result = replace(result, "SHORT", "DD/MM/YY");
			result = replace(result, "MEDIUM", "MMM, DD, YYYY");
			result = replace(result, "LONG", "MMMM, DD, YYYY");
			result = replace(result, "FULL", "DDDD, MMMM, DD, YYYY");
			
			result = replace(result, "YYYY", mid(fString,1,4));
			result = replace(result, "YY", mid(fString,3,2));
			
			result = replace(result, "MMMM", fullMonth[mid(fString,5,2)]);
			result = replace(result, "MMM", shortMonth[mid(fString,5,2)]);
			result = replace(result, "MM", mid(fString,5,2));
			
			result = replace(result, "DDDD", fullWeekday[fWeekDay]);
			result = replace(result, "DDD", shortWeekday[fWeekDay]);
			result = replace(result, "DD", mid(fString,7,2));
			
			result = replace(result, "HH", mid(fString,9,2));
			result = replace(result, "MI", mid(fString,11,2));
			result = replace(result, "SS", mid(fString,13,2));
			
			if (fHour GTE 0 AND fHour LT 12) {
				result = replace(result, "TT", "AM");
			}
			else {
				result = replace(result, "TT", "PM");
			}
		}
		catch(any excpt) {
			result = "";
		}
		return result;
		
	}
	
	




}
</cfscript>